//
//  UIColor+.swift
//  ScarnKing
//
//  Created by ninebot on 2025/7/22.
//

import Foundation
import UIKit
public extension UIColor {

    class func stec_color(withHexString hex:String) -> UIColor {
        return UIColor.stec_color(withHexString: hex, alpha: 1)
    }
    
    class func stec_color(withHex hex: Int) -> UIColor {
        return UIColor.stec_color(withHex: hex, alpha: 1)
    }

    class func stec_black(withAlpha alpha: CGFloat) -> UIColor {
        return UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: alpha)
    }
    class func stec_color(withHex hex: Int, alpha: CGFloat) -> UIColor{
        return UIColor(red: ((CGFloat)((hex & 0xFF0000) >> 16) / 255.0), green: ((CGFloat)((hex & 0xFF00) >> 8)) / 255.0, blue: ((CGFloat)(hex & 0xFF)) / 255.0, alpha: alpha)
    }
    class func stec_color(withHexString hex:String, alpha: CGFloat) -> UIColor{
        var cString:String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines).uppercased()
        //stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercased()
        if (cString.hasPrefix("#")) {
            cString = (cString as NSString).substring(from: 1)
        }
        if (cString.count != 6) {
            return UIColor.gray
        }
        let rString = (cString as NSString).substring(to: 2)
        let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
        let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)
        
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)
        return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: alpha)
    }
    
    class func stec_randomColor() -> UIColor {
        return UIColor(red: CGFloat(Double(arc4random_uniform(100)) * 0.01), green: CGFloat(Double(arc4random_uniform(100)) * 0.01), blue: CGFloat(Double(arc4random_uniform(100)) * 0.01), alpha: 1)
    }
}
